import 'dart:collection';

enum ModuleInitType {
  migrattion,
  block,
  opts,
  qom,
  trace,
  xenBackend,
  libQos,
  fuzzTarget,
  max
}

final class _ModuleEntry extends LinkedListEntry<_ModuleEntry> {
  ModuleInitType type;
  Function init;
  _ModuleEntry(this.type, this.init);
}

class _ModuleType {
  static bool inited = false;
  final LinkedList<_ModuleEntry> entry = LinkedList();
}

late List<_ModuleType> _initTypeList;

List<bool> _modulesInitDone =
    List.generate(ModuleInitType.max.index, (index) => false);

void _initLists() {
  if (_ModuleType.inited) return;
  _initTypeList =
      List.generate(ModuleInitType.max.index, (index) => _ModuleType());

  _ModuleType.inited = true;
}

_ModuleType _findType(ModuleInitType type) {
  _initLists();
  return _initTypeList[type.index];
}

void moduleCallInit(ModuleInitType type) {
  if (_modulesInitDone[type.index]) return;

  _ModuleType t = _findType(type);

  for (var entry in t.entry) {
    entry.init.call();
  }

  _modulesInitDone[type.index] = true;
}
